package com.example.xymarket.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.xymarket.common.Info;
import com.example.xymarket.myView.StatusBar;
import com.example.xymarket.R;
import com.example.xymarket.httpUtil.UserAddressService;
import com.example.xymarket.common.MyHandler;

import java.util.HashMap;
import java.util.Map;

public class AddAddressActivity extends AppCompatActivity {

    EditText phone;
    EditText name;
    EditText address;
    Integer id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        phone = findViewById(R.id.phone_input);
        address = findViewById(R.id.address_input);
        name = findViewById(R.id.name_input);
        StatusBar.setStatusBarColor(this,StatusBar.yellow);
        if (bundle != null) {
            id = Integer.parseInt((String) bundle.get("id"));
            phone.setText((String) bundle.get("phone"));
            name.setText((String) bundle.get("name"));
            address.setText((String) bundle.get("address"));
        }
    }

    @SuppressLint("HandlerLeak")
    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 2:
                {
                    Toast.makeText(getApplicationContext(),"保存成功",Toast.LENGTH_LONG).show();
                    Intent intent=new Intent(getApplicationContext(),AddressActivity.class);
                    startActivity(intent);
                    overridePendingTransition(0, 0);
                    break;
                }
                case -2:
                {
                    Toast.makeText(getApplicationContext(),"操作失败",Toast.LENGTH_LONG).show();
                    break;
                }
            }
        }
    };

    public void saveAddress(View view)
    {
        MyHandler myHandler=new MyHandler(getApplicationContext(),handler);
        UserAddressService userAddressService=new UserAddressService(myHandler);
        Map<String,String> map=new HashMap<>();
        if("id"!=null) {
            map.put("id", String.valueOf(id));
        }

        map.put("user_id",String.valueOf(Info.getMyUserInfo().getId()));
        map.put("phone",phone.getText().toString());
        map.put("name",name.getText().toString());
        map.put("address",address.getText().toString());
        userAddressService.saveAddress(map);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            Intent intent = new Intent(getApplicationContext(), AddressActivity.class);
            startActivity(intent);overridePendingTransition(0, 0);
        }
        return super.onKeyDown(keyCode, event);
    }

}